﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ProcessMonitoringCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ProcessMonitoringHandler
{
    public class DelProcessMonitoringHandler : IRequestHandler<DelProcessMonitoringCommand, APIResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public DelProcessMonitoringHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
       
        public async Task<APIResult<int>> Handle(DelProcessMonitoringCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list = await db.Queryable<ProcessMonitoring>().FirstAsync(x => x.id == request.id);

            // 增加判空逻辑
            if (list == null)
            {
                result.code = APIResultCode.failed;
                result.msg = "未找到要删除的过程监控";
                return result;
            }

            list.IsDel = true; 
            result.data = await db.Updateable<ProcessMonitoring>(list).ExecuteCommandAsync();
            if (result.data > 0)
            {
                result.code = APIResultCode.success;
                result.msg = " 过程监控删除成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "过程监控删除失败";
            }
            return result;
        }
    }
}
